// Widgets/Protocol/ProtocolOther/ProtocolOther.js
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    otherPromises: {
      type: String, // 其他约定
    },
    otherImgUrlList: {
      type: Array, // 其他约定图片
    }
  },
  options: {
    styleIsolation: 'shared',
  },

  /**
   * 组件的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    uploadFileUrl: app.globalData.global.uploadFileUrl,
    brandId: app.globalData.global.brandId,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 选择照片
    selImg(e){
      let _this = this;
      wx.chooseMedia({
        count: 1,
        mediaType: ['image'],
        success(e){
          console.log(e.tempFiles)
          var token = wx.getStorageSync('access_token');
          var timeStamp = new Date().getTime();
          wx.uploadFile({
            url: _this.data.uploadFileUrl, // 上传接口
            filePath: e.tempFiles[0].tempFilePath,
            name: 'file',
            formData: { 'brandId': _this.data.brandId ,'access_token': token,'timeStamp': timeStamp},
            success(res) {
              console.log(res,'res')
              var res = JSON.parse(res.data);
              if(res.success){
                _this.triggerEvent('updateImgList',{'data': res.data,'name': 'otherImgUrlList'})
              }else{
                wx.showToast({
                  title: res.errMessage,
                  icon: 'none',
                })
              }
            },
          });
        },
        fail: function(err){
          console.log(err,'err')
        }
      })
    },
    // 删除图片
    delListImg(e){
      let _this = this;
      let name = e.target.dataset.name||e.currentTarget.dataset.name;
      let index = e.target.dataset.index||e.currentTarget.dataset.index;
      _this.triggerEvent('updateImgList',{'data': '','name': name,'index': index})
    },
    onChange(event){
      // event.detail 为当前输入的值
      console.log(event.detail);
      const name = event.target.dataset.name||event.currentTarget.dataset.name;
      console.log(name,'name')
      this.triggerEvent('updateValue',{'data': event.detail,'name': name})
    },
  }
})